﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.DataModel;
using System.IO;
using System.Diagnostics;
using SharedLib;
using Microsoft.Win32;
using System.Collections.Concurrent;
using Tracy.Messages;
using System.Collections.Specialized;

namespace Tracy.Plugins.DownloadAdapters
{
    public class EmuleAdapter : IDownloadAdapter
    {
        private string _installFolder;
        private string _applicationFullPath;
        private bool _notifyOnNewDownload = true;
        private string _incomingDir = null;

        private void CheckRegistry(){
            RegistryKey regdir = Registry.CurrentUser.OpenSubKey("SOFTWARE", false);
            regdir = regdir.OpenSubKey("easyMule", false);
            if (regdir != null)
            {
                //Console.WriteLine("easyMule Registry Key found");
                object installPath = regdir.GetValue("InstallPath");
                object fileName = regdir.GetValue("FileName");
                if (installPath != null && fileName != null)
                {
                    _installFolder = installPath.ToString();
                    _applicationFullPath = installPath.ToString() + fileName.ToString();
                    //Console.WriteLine("easyMule exe found: " + rtn);
                    if (!File.Exists(_applicationFullPath)) _applicationFullPath = null;
                }
            }
        }
        public void Init(StringDictionary parameters) { }

        private void ParsePreferences()
        {
            string preferencesFileName = _installFolder + "config\\preferences.ini";
            if (File.Exists(preferencesFileName))
            {
                string line = "";
                StreamReader sr = new StreamReader(preferencesFileName);
                try
                {
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();
                        if (line.StartsWith("NewTaskDlg"))
                        {
                            _notifyOnNewDownload = (line.Substring(11) == "1"); 
                        }
                        else if (line.StartsWith("IncomingDir"))
                        {
                            _incomingDir = line.Substring(12);
                            if (!_incomingDir.EndsWith("\\")) _incomingDir += "\\";
                        }
                    }
                }
                finally
                {
                    sr.Close();
                }


            }

        }
        public EmuleAdapter()
        {
            CheckRegistry();
            ParsePreferences();
        }


        public void Download(Resource resource, string defaultPath)
        {
            if (resource == null) return;
            if (IsResourceTypeSupported(resource.GetResourceType()))
            {
                Process.Start(_applicationFullPath, "\"" + resource.Url + "\"");

                //if (_notifyOnNewDownload)
                //{
                    //resource.Properties["default-path"] = defaultPath;
                    //resource.Properties["auto-mode"] = autoMode ? "true" : "false";
                    //TracyBus.Instance.MessageDispatcher.EnqueueMessage(new TaskMessage(this, typeof(EmuleNewTaskDialogHandler), resource, null));
                //}
            }
        }


        public bool IsAvailable()
        {
            return _applicationFullPath != null;
        }

        public bool IsResourceTypeSupported(string type)
        {
            if (IsAvailable())
            {
                if (type == "Emule") return true;
            }
            return false;
        }

        public string[] GetTempFileExts(string type)
        {
            if (type == "Emule") return new string[]{ ".part"};
            return null;
        }

        public string GetValidFileName(string fileName)
        {
            if(fileName == null)return null;
            return fileName.Replace("?", "-").Replace("\\", "-").Replace("/", "-").Replace("*", "-").Replace("\"", "-").Replace(":", "-").Replace("<", "-").Replace(">", "-");
        }


        public string GetTempDownloadFolder(string preferredFolder)
        {
            //if (!_notifyOnNewDownload) return _incomingDir;
            //return preferredFolder;
            return _incomingDir;
        }
    }
}
